package com.xudow.app.dynamicstate_old.module.user;

import android.Manifest;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.jude.beam.bijection.RequiresPresenter;
import com.jude.beam.expansion.BeamBaseActivity;
import com.jude.tagview.TAGView;
import com.jude.utils.JUtils;
import com.squareup.picasso.Picasso;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.xudow.app.R;
import com.xudow.app.dynamicstate_old.data.ImageModel;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ske on 2016/6/16.
 */
@RequiresPresenter(PhotoChangePresenter.class)
public class PhotoChangeActivity extends BeamBaseActivity<PhotoChangePresenter> {
    @BindView(R.id.get)
    TAGView get;
    @BindView(R.id.ok)
    TAGView ok;
    @BindView(R.id.cancel)
    TAGView cancel;
    @BindView(R.id.image)
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_change);
        ButterKnife.bind(this);

        setup();
    }

    private void setup() {
        String cover = getIntent().getStringExtra("coverPhoto");

        get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSelectorDialog();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPresenter().changeCover();
            }
        });
        if (getIntent().getBooleanExtra("isAvatar", true)) {
            get.setText("更换头像");
            Picasso.with(this).load(cover).into(image);
        } else {
            get.setText("更换封面");
            if (!TextUtils.isEmpty(cover))
                Picasso.with(this)
                        .load(cover)
                        .into(image);
        }
    }

    public void showSelectorDialog() {
        RxPermissions.getInstance(this).request(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(enable -> {
                    if (enable) {
                        new MaterialDialog.Builder(this)
                                .title("选择图片来源")
                                .items(new String[]{"拍照", "相册"})
                                .itemsCallback(new MaterialDialog.ListCallback() {
                                    @Override
                                    public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                                        getPresenter().editFace(which);
                                    }
                                }).show();
                    } else {
                        JUtils.Toast("请授予权限进行操作");
                    }
                });
    }

    public void setImage(Uri uri) {
        Glide.with(this).load(uri).into(image);
    }
}
